<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ZHOUYANG
 * Date: 13-3-30
 * Time: 下午2:37
 */
 
class RoleController extends Controller{

    public function index(){

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);

        $list = Model::getInstance('role')->getRoleList($curr_page,$per_page);
        $data = array(
            'list'=>$list,
        );

        $template = array('data'=>$data);
        Template::show($template);
    }

    public function listShow(){

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);

        $list = Model::getInstance('role')->getRoleList($curr_page,$per_page);

        $parent_rel  = $this->input()->get('rel');
        $data = array(
            'parent_rel'=>$parent_rel,
            'list'=>$list,
        );

        $template = array('data'=>$data);
        Template::show($template);
    }

    public function add(){
        Template::show();
    }

    public function del(){

        $role_id = $this->input()->get('role_id');
        $ret = Model::getInstance('role')->delRole($role_id);

        if($ret){
            Model::getInstance('catehasrole')->delByRoleId($role_id);
            Model::getInstance('userhasrole')->delByRoleId($role_id);
            $data = array(
                "statusCode"=>"200",
                "navTabId"=>"role",
            );
            echo Response::JSON($data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON($data);
        }
    }

    public function edit(){

        $role_id = (int)$this->input()->get('role_id');

        $role_info = Model::getInstance('role')->get($role_id);
        $data = array(
            'role_info'=>$role_info,
        );

        $template = array('data'=>$data);
        Template::show($template);
    }

    public function cateTree(){
        
        $role_id = (int)$this->input()->get('role_id');

        $roles = Model::getInstance('role')->getRoleCates($role_id);

        $cate_ids = array();
        foreach($roles as $v){
            $cate_ids[] = $v['cate_id'];
        }

        $menu_arr = Model::getInstance('cate')->getTree();
        
        $data = array(
            'role_id'=>$role_id,
            'menu_arr'=>$menu_arr,
            'cate_ids'=>$cate_ids,
        );

        $template = array('data'=>$data);
        Template::show($template);
    }
}